import Vue from "vue";

//可拖动弹窗
Vue.directive("DragWindow" , {
    inserted(el , bind){
     //inserted 保证渲染到了页面当中
     console.log(el);
     console.log(bind);
     let elDialog = el.getElementsByClassName("el-dialog")[0];
     let elDialogHeader = el.getElementsByClassName("el-dialog__header")[0];
     elDialogHeader.style.cursor = "move";
     //获取当前弹窗的所有样式
     let style = elDialog.currentStyle || window.getComputedStyle(elDialog, null);
    //  console.log(style);
     // 弹窗的头部按下的时候进行拖拽
     elDialogHeader.onmousedown = (e)=>{
         let disX = e.clientX - elDialogHeader.offsetLeft;
         let disY = e.clientY - elDialogHeader.offsetTop;
         let styL;
         let styT;
         styL =+ style.left.replace(/\px/g,'');
         styT =+ style.top.replace(/\px/g,'');
         document.onmousemove = function(e){
             let l = e.clientX - disX;
             let t = e.clientY -disY;
             elDialog.style.left = `${l +styL}px`;
             elDialog.style.top = `${t +styT}px`;
         }
         document.onmouseup = function(){
             document.onmouseup = null;
             document.onmousemove = null;
         }
     }
    }
 })
 


// 防抖
export function _debounce(fn, delay) {
    var delay = delay || 200;
    var timer;
    return function () {
        var th = this;
        var args = arguments;
        if (timer) {
            clearTimeout(timer);
        }
        timer = setTimeout(function () {
            timer = null;
            fn.apply(th, args);
        }, delay);
    };
}
// 节流
export function _throttle(fn, interval) {
    var last;
    var timer;
    var interval = interval || 200;
    return function () {
        var th = this;
        var args = arguments;
        var now = +new Date();
        if (last && now - last < interval) {
            clearTimeout(timer);
            timer = setTimeout(function () {
                last = now;
                fn.apply(th, args);
            }, interval);
        } else {
            last = now;
            fn.apply(th, args);
        }
    }
}